import message from '@/logic/message'
import {AppModule} from '@/logic/AppModule'
import {PageModule} from '@/logic/PageModule'

//资源加载器
class Loader{
    constructor() {
        this.staticModules={};
    }

    loadResource(opts) {
        //加载静态资源
        const {appId,bridgeId,pages}=opts;
        const logicResourcePath=`http://127.0.0.1:3200/mini_resource/${appId}/logic/logic.js`;
        importScripts(logicResourcePath);//加载逻辑资源
        modRequire('app');
        pages.forEach(pagePath => {
            modRequire(pagePath);
        });
        message.send({
            type:'logicResuorceLoaded',
            body:{
                bridgeId
            }
        })
    }
    getModuleByPath(path){
        return this.staticModules[path];
    }
    createAppModule(moduleInfo){
        const appModule=new AppModule();
        this.staticModules.app=appModule;
    }
    createPageModule(moduleInfo,compileInfo){
        const pageModule=new PageModule(moduleInfo,compileInfo);
        const {path}=compileInfo;
        this.staticModules[path]=pageModule;
    }
    getInitialDataByPagePath(pagePath){
        const pageModule=this.staticModules[pagePath];
        return {[pagePath]:pageModule.getInitialData()};
    }

}
export default new Loader();